<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字幕测试工具</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background-color: #121212;
            color: white;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        
        h1 {
            color: #ffffff;
            margin-bottom: 30px;
        }
        
        .test-container {
            background: rgba(255, 255, 255, 0.1);
            padding: 20px;
            border-radius: 10px;
            width: 80%;
            max-width: 600px;
            margin-bottom: 30px;
        }
        
        .test-input {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 5px;
            background: rgba(0, 0, 0, 0.5);
            color: white;
            font-size: 16px;
        }
        
        .test-buttons {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        
        .test-btn {
            padding: 10px 20px;
            background: #6200EA;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s;
        }
        
        .test-btn:hover {
            background: #4B0082;
        }
        
        .subtitle-status {
            margin-top: 20px;
            padding: 10px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 5px;
            width: 80%;
            max-width: 600px;
        }
        
        /* 字幕样式 - 与主应用保持一致 */
        #subtitles {
            position: fixed;
            bottom: 100px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(0, 0, 0, 0.85);
            color: white;
            padding: 15px 25px;
            border-radius: 15px;
            font-size: 20px;
            font-weight: 500;
            max-width: 75%;
            min-height: 60px;
            text-align: center;
            z-index: 30;
            opacity: 0;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            line-height: 1.4;
            letter-spacing: 0.5px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        #subtitle-text {
            color: white;
            font-size: inherit;
            font-weight: inherit;
            line-height: inherit;
            letter-spacing: inherit;
            text-align: center;
            word-wrap: break-word;
            max-height: 100%;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        #subtitles.show {
            opacity: 1;
            transform: translateX(-50%) translateY(-5px);
        }
        
        #subtitles.realtime {
            animation: subtitlePulse 0.1s ease-in-out;
        }
        
        @keyframes subtitlePulse {
            0% { transform: translateX(-50%) translateY(-5px) scale(1); }
            50% { transform: translateX(-50%) translateY(-5px) scale(1.01); }
            100% { transform: translateX(-50%) translateY(-5px) scale(1); }
        }
        
        /* 调试信息样式 */
        .debug-info {
            margin-top: 20px;
            width: 80%;
            max-width: 600px;
            background: rgba(0, 0, 0, 0.5);
            padding: 15px;
            border-radius: 5px;
            max-height: 300px;
            overflow-y: auto;
            font-family: 'Courier New', Courier, monospace;
            font-size: 14px;
        }
        
        .debug-line {
            margin-bottom: 5px;
            color: #888;
        }
        
        .debug-line.info {
            color: #34C759;
        }
        
        .debug-line.warning {
            color: #FF9500;
        }
        
        .debug-line.error {
            color: #FF3B30;
        }
    </style>
</head>
<body>
    <h1>字幕测试工具</h1>
    
    <div class="test-container">
        <input type="text" id="subtitle-input" class="test-input" placeholder="输入要显示的字幕文本..." value="这是一条测试字幕，您可以在这里输入任何文本">
        <div class="test-buttons">
            <button id="test-basic-subtitle" class="test-btn">显示基本字幕</button>
            <button id="test-realtime-subtitle" class="test-btn">实时逐字显示</button>
            <button id="test-clear-subtitle" class="test-btn">清除字幕</button>
            <button id="test-check-elements" class="test-btn">检查元素</button>
        </div>
    </div>
    
    <div class="subtitle-status">
        <h3>字幕状态</h3>
        <p>点击上方按钮测试字幕功能。请打开浏览器开发者工具（F12）查看详细日志。</p>
    </div>
    
    <div class="debug-info" id="debug-info">
        <div class="debug-line info">测试工具已加载完成</div>
    </div>
    
    <!-- 字幕容器 - 与主应用结构保持一致 -->
    <div id="subtitles">
        <div id="subtitle-text"></div>
    </div>
    
    <script>
        // 获取DOM元素
        const subtitlesContainer = document.getElementById('subtitles');
        const subtitleTextElement = document.getElementById('subtitle-text');
        const subtitleInput = document.getElementById('subtitle-input');
        const basicSubtitleBtn = document.getElementById('test-basic-subtitle');
        const realtimeSubtitleBtn = document.getElementById('test-realtime-subtitle');
        const clearSubtitleBtn = document.getElementById('test-clear-subtitle');
        const checkElementsBtn = document.getElementById('test-check-elements');
        const debugInfo = document.getElementById('debug-info');
        
        // 调试日志函数
        function logDebug(message, type = 'info') {
            const timestamp = new Date().toLocaleTimeString();
            const logLine = document.createElement('div');
            logLine.className = `debug-line ${type}`;
            logLine.textContent = `[${timestamp}] ${message}`;
            debugInfo.appendChild(logLine);
            debugInfo.scrollTop = debugInfo.scrollHeight;
            
            // 同时输出到控制台
            switch(type) {
                case 'error':
                    console.error(message);
                    break;
                case 'warning':
                    console.warn(message);
                    break;
                default:
                    console.log(message);
            }
        }
        
        // 检查元素状态
        function checkElements() {
            logDebug('开始检查DOM元素...');
            
            // 检查字幕容器
            if (subtitlesContainer) {
                logDebug(`字幕容器存在: id="${subtitlesContainer.id}", class="${subtitlesContainer.className}"`, 'info');
                logDebug(`容器样式: opacity=${window.getComputedStyle(subtitlesContainer).opacity}`, 'info');
                logDebug(`容器位置: bottom=${window.getComputedStyle(subtitlesContainer).bottom}`, 'info');
            } else {
                logDebug('未找到字幕容器元素', 'error');
            }
            
            // 检查字幕文本元素
            if (subtitleTextElement) {
                logDebug(`字幕文本元素存在: id="${subtitleTextElement.id}"`, 'info');
                logDebug(`文本内容: "${subtitleTextElement.textContent}"`, 'info');
            } else {
                logDebug('未找到字幕文本元素', 'error');
            }
            
            // 检查show类
            const hasShowClass = subtitlesContainer.classList.contains('show');
            logDebug(`当前是否有show类: ${hasShowClass}`, 'info');
        }
        
        // 显示基本字幕
        function showBasicSubtitle(text) {
            logDebug(`显示基本字幕: "${text}"`, 'info');
            
            // 清除之前的状态
            subtitlesContainer.classList.remove('realtime');
            
            // 确保字幕文本元素存在
            if (subtitleTextElement) {
                subtitleTextElement.textContent = text;
                logDebug(`已设置subtitle-text内容`, 'info');
            } else {
                subtitlesContainer.textContent = text;
                logDebug(`未找到subtitle-text元素，直接设置容器内容`, 'warning');
            }
            
            // 显示字幕
            subtitlesContainer.classList.add('show');
            logDebug(`已添加show类`, 'info');
            
            // 3秒后自动隐藏
            setTimeout(() => {
                subtitlesContainer.classList.remove('show');
                logDebug(`已移除show类，字幕隐藏`, 'info');
            }, 3000);
        }
        
        // 实时逐字显示字幕
        function showRealtimeSubtitle(text) {
            logDebug(`开始实时逐字显示: "${text}"`, 'info');
            
            // 清除之前的状态
            subtitleTextElement.textContent = '';
            subtitlesContainer.classList.remove('show');
            subtitlesContainer.classList.remove('realtime');
            
            // 设置最大显示长度
            const maxDisplayLength = 20;
            
            // 计算字符显示间隔
            const audioDuration = 5; // 假设音频时长为5秒
            const charInterval = Math.max(20, (audioDuration * 0.95) / text.length);
            
            logDebug(`计算字符间隔: ${charInterval}ms`, 'info');
            
            // 显示容器
            subtitlesContainer.classList.add('show');
            subtitlesContainer.classList.add('realtime');
            logDebug(`已添加show和realtime类`, 'info');
            
            let currentIndex = 0;
            
            // 设置逐字显示定时器
            const realtimeTimer = setInterval(() => {
                if (currentIndex < text.length) {
                    // 获取当前要显示的字符
                    const char = text[currentIndex];
                    
                    // 更新显示的文本
                    let currentText = subtitleTextElement.textContent;
                    
                    // 如果超过最大显示长度，移除最前面的字符
                    if (currentText.length >= maxDisplayLength && char !== '\n') {
                        currentText = currentText.substring(1);
                    }
                    
                    // 添加新字符
                    currentText += char;
                    subtitleTextElement.textContent = currentText;
                    
                    logDebug(`已显示字符 ${currentIndex + 1}/${text.length}: "${currentText}"`, 'info');
                    
                    currentIndex++;
                } else {
                    // 文本显示完成
                    clearInterval(realtimeTimer);
                    subtitlesContainer.classList.remove('realtime');
                    logDebug(`实时显示完成，已移除realtime类`, 'info');
                    
                    // 显示完整字幕一段时间后隐藏
                    setTimeout(() => {
                        subtitlesContainer.classList.remove('show');
                        logDebug(`字幕隐藏`, 'info');
                    }, 2000);
                }
            }, charInterval);
        }
        
        // 清除字幕
        function clearSubtitle() {
            logDebug('清除字幕', 'info');
            subtitlesContainer.classList.remove('show', 'realtime');
        }
        
        // 绑定事件
        basicSubtitleBtn.addEventListener('click', () => {
            const text = subtitleInput.value || '这是一条测试字幕';
            showBasicSubtitle(text);
        });
        
        realtimeSubtitleBtn.addEventListener('click', () => {
            const text = subtitleInput.value || '这是一条实时逐字显示的测试字幕';
            showRealtimeSubtitle(text);
        });
        
        clearSubtitleBtn.addEventListener('click', clearSubtitle);
        
        checkElementsBtn.addEventListener('click', checkElements);
        
        // 初始检查元素
        setTimeout(checkElements, 1000);
        
        logDebug('所有事件监听器已绑定，测试工具准备就绪', 'info');
    </script>
</body>
</html>